﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace Serializer
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * 调用MySerializer类
             */
            //使用类序列化json
            Book bookJsonClass = new Book() { ID = 103, Name = "Book类序列化为Json", Price = 23.5f };
            string jsonResultClass = MySerializer.ObjectToJson<Book>(bookJsonClass);
            Console.WriteLine(jsonResultClass);

            MySerializer.ObjectToJson<Book>(bookJsonClass, @"JsonClass.txt");

            //使用类反序列化json
            string strJsonDeClass = "{\"ID\":104,\"Name\":\"使用类Json反序列化\",\"Price\":23.5}";
            Book jsonBookResultClass = MySerializer.JsonToObject<Book>(strJsonDeClass);
            Console.WriteLine(jsonBookResultClass.ID);
            Console.WriteLine(jsonBookResultClass.Name);
            Console.WriteLine(jsonBookResultClass.Price);
            Console.WriteLine();

            Book jsonBookClass = new Book();
            MySerializer.JsonToObject<Book>(out jsonBookClass, @"JsonClass.txt");
            Console.WriteLine(jsonBookClass.ID);
            Console.WriteLine(jsonBookClass.Name);
            Console.WriteLine(jsonBookClass.Price);
            Console.WriteLine();

            //使用类序列化xml
            Book bookXmlClass = new Book() { ID = 106, Name = "使用类序列化xml", Price = 12.5f };
            string xmlResultClass = MySerializer.ObjectToXml<Book>(bookXmlClass);
            Console.WriteLine(xmlResultClass);

            MySerializer.ObjectToXml(bookXmlClass, @"bookXmlClass.xml");

            //使用类反序列化xml
            Book xmlBookResultClass = MySerializer.XmlToObject<Book>(@"bookXml.xml");
            Console.WriteLine(xmlBookResultClass.ID);
            Console.WriteLine(xmlBookResultClass.Name);
            Console.WriteLine(xmlBookResultClass.Price);
            Console.WriteLine();

            //使用类序列化文件
            Book bookBinaryClass = new Book() { ID = 106, Name = "使用类序列化文件", Price = 12.5f };
            string binaryResult = MySerializer.ObjectToString<Book>(bookBinaryClass);
            Console.WriteLine(binaryResult);

            MySerializer.ObjectToFile<Book>(bookBinaryClass, @"bookBinary.txt");

            //使用类反序列化文件
            //Book bookBinaryDe = MySerializer.StringToObject<Book>(binaryResult);
            //Console.WriteLine(bookBinaryDe.ID);
            //Console.WriteLine(bookBinaryDe.Name);
            //Console.WriteLine(bookBinaryDe.Price);
            //Console.WriteLine();

            Book binaryResultDe = MySerializer.FileToObject<Book>(@"bookBinary.txt");
            Console.WriteLine(binaryResultDe.ID);
            Console.WriteLine(binaryResultDe.Name);
            Console.WriteLine(binaryResultDe.Price);
            Console.WriteLine();

            //byte[] bookByte = SerializeHelper.SerializeToBinary(bookBinaryClass);
            //string bookByteToStr = SerializeHelper.ConvertToString(bookByte);
            //Console.WriteLine(bookByteToStr);

            //byte[] bookByteDe = SerializeHelper.ConvertToByte(bookByteToStr);
            //Book byteToBook = SerializeHelper.DeserializeWithBinary<Book>(bookByteDe);
            //Console.WriteLine(byteToBook.ID);
            //Console.WriteLine(byteToBook.Name);
            //Console.WriteLine(byteToBook.Price);
            //Console.WriteLine();

        }
    }
}
